package com.dlc.felear.xiaoensale.helper;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.dlc.felear.xiaoensale.dlc_serial_port.DlcSerialPortClient;
import com.dlc.felear.xiaoensale.entity.NextStep;
import com.dlc.felear.xiaoensale.factory.CmdList;
import com.dlc.felear.xiaoensale.utils.RecordLog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

public class YetiTiaoLiaoHelper implements Handler.Callback{
    private static final String TAG="YetiTiaoLiaoHelper";
    private static final int WHAT_1=1347;
    private final int IO_CLOSE_VALUE = 0;
    private final int IO_OPEN_VALUE = 255;
    private String serialNum;
    private byte io_base=18;
    private Handler handler;
    private GuotiHelper guotiHelper;
    private IoOutHelper ioOutHelper;
    private ScheduledExecutorService scheduledThreadPool;
    private List<NextStep> nextSteps;

    public YetiTiaoLiaoHelper(ScheduledExecutorService scheduledThreadPool, IoOutHelper ioOutHelper) {
        this.ioOutHelper = ioOutHelper;
        this.serialNum = serialNum;
        this.scheduledThreadPool = scheduledThreadPool;
        handler=new Handler(this);
        nextSteps=new ArrayList<>();
    }


    public void setGuotiHelper(GuotiHelper guotiHelper) {
        this.guotiHelper = guotiHelper;
    }

    public void paserData(final byte num, final int msec){
        log("液体序号:"+num+" delay="+msec,0);
        scheduledThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    if(guotiHelper.getPosition((byte) 2)==1){
                        log("锅体在中间,进行加液体调料动作",0);
                        controlOutputIo((byte) (io_base+num),IO_OPEN_VALUE);
                        sendHandlerMsg(WHAT_1,(byte) (io_base+num),msec);
                    }else {
                        nextSteps.add(new NextStep((byte) (io_base+num), (byte) IO_OPEN_VALUE, msec));
                        log("锅体不在中间，为了防止调料洒出，暂停加液体调料动作",1);
                    }
                }catch (Exception e){

                }
            }
        });

    }

    /**
     * io状态控制
     *
     * @param num
     * @param value
     */
    private void controlOutputIo(byte num, int value) {
        if (num >= 26) {
            sendCmd(serialNum, CmdList.controlOutputIo(3, num - 26, value));
        } else {
            sendCmd(serialNum,CmdList.controlOutputIo(2, num, value));
        }
    }

    private void sendCmd(String serialNo,byte[] data) {
        ioOutHelper.addCmdToQueque(data);
    }




    @Override
    public boolean handleMessage(Message msg) {
        if(WHAT_1==msg.what){
            controlOutputIo((byte) msg.arg1,IO_CLOSE_VALUE);
        }
        return false;
    }

    public void closeAction(){
        for (int i = 18; i <26 ; i++) {
            controlOutputIo((byte) i,0);
        }
    }

    private void sendHandlerMsg(int what,byte num,long delayTime){
        Message message=new Message();
        message.arg1=num;
        message.what=what;
        handler.sendMessageDelayed(message,delayTime);
    }

    private void log(String msg,int level){
        if(level==1){
            RecordLog.getInstance().saveLog(msg);
        }
        Log.e(TAG,msg);
    }

    public void sensor(byte num) {
        if(num!=2){
            return;
        }
        handlerYttl();
    }

    private void handlerYttl() {
        scheduledThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    long temp = 0;
                    if (!nextSteps.isEmpty()) {
                        for (NextStep nextStep : nextSteps) {
                            log("延迟加液体调料:"+nextStep.getDelayTime(),0);
                            controlOutputIo(nextStep.getNum(),IO_OPEN_VALUE);
                            sendHandlerMsg(WHAT_1,nextStep.getNum(),nextStep.getDelayTime());
                            Thread.sleep(1000);
                            nextSteps.remove(nextStep);
                        }
                    }
                }catch (Exception e){

                }
            }
        });
    }
}
